package com.haixiaoke.saas.enterprise.house.controller;

import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.department.domain.Tenant;
import com.haixiaoke.saas.department.service.ITenantService;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.order.domain.Order;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 房屋信息Controller
 *
 * @author panda
 * @date 2023-10-18
 */
@RestController
@RequestMapping("/enterprise/house/house")
public class EnterpriseHouseController extends BaseController {
    @Autowired
    private IHouseService houseService;
    @Autowired
    private ITenantService tenantService;

    /**
     * 查询房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:list')")
    @GetMapping("/list")
    @TenantScope
    public TableDataInfo list(House house) {
        startPage();
        house.setTenantId(TenantContext.getTenantId());
        List<House> list = houseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 查询房屋信息列表
     */
    @GetMapping("/page")
    @TenantScope
    public TableDataInfo page(House house) {
        startPage();
        house.setTenantId(TenantContext.getTenantId());
        List<House> list = houseService.selectHousePage(house);
        return getDataTable(list);
    }

    /**
     * 根据订单入离时间查询房屋信息列表
     */
    @GetMapping("/pageByOrder")
    @TenantScope
    public TableDataInfo pageByOrder(Order order) {
        startPage();
        order.setTenantId(TenantContext.getTenantId());
        return houseService.pageByOrder(order);
    }

    @GetMapping("/statistics")
    @TenantScope
    public AjaxResult statistics(){
        return success(houseService.statistics());
    }

    @GetMapping("/countHouseState")
    @TenantScope
    public AjaxResult countHouseState(){
        return success(houseService.countHouseState());
    }

    /**
     * 导出房屋信息列表
     */
    @PreAuthorize("@ss.hasPermi('house:house:export')")
    @PostMapping("/export")
    @TenantScope
    public void export(HttpServletResponse response, House house) {
        house.setTenantId(TenantContext.getTenantId());
        List<House> list = houseService.selectHouseList(house);
        ExcelUtil<House> util = new ExcelUtil<House>(House.class);
        util.hideColumn("estateId","provinceId","cityId","districtId","streetId","communityId","propertyId");
        util.exportExcel(response, list, "房屋信息数据");
    }

    /**
     * 获取房屋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") String houseId) {
        return success(houseService.selectHouseByHouseId(houseId));
    }

    /**
     * 新增房屋信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:add')")
    @PostMapping
    @TenantScope
    public AjaxResult add(@RequestBody House house) {
        if (houseService.checkHouse(house)){
            return AjaxResult.error("该房屋已存在");
        }
        String tenantId = TenantContext.getTenantId();

        //检验房屋数量
        tenantService.checkHouseNum(tenantId);

        Long userId = getUserId();
        house.setCreateBy(String.valueOf(userId));
        house.setOperatorId(userId);
        house.setTenantId(tenantId);
        LoginUser loginUser = getLoginUser();
        house.setBizUserBusiness(loginUser.getBizUser());
        return toAjax(houseService.insertHouse(house));
    }

    /**
     * 修改房屋信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:edit')")
    @PutMapping
    @TenantScope
    public AjaxResult edit(@RequestBody House house) {
        if (houseService.checkHouse(house)){
            return AjaxResult.error("该房屋已存在");
        }
        Long userId = getUserId();
        house.setUpdateBy(String.valueOf(userId));
        LoginUser loginUser = getLoginUser();
        house.setBizUserBusiness(loginUser.getBizUser());
        return toAjax(houseService.updateHouse(house));
    }

    /**
     * 删除房屋信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:remove')")
	@DeleteMapping("/{houseIds}")
    @TenantScope
    public AjaxResult remove(@PathVariable String[] houseIds) {
        return toAjax(houseService.deleteHouseByHouseIds(houseIds));
    }

    /**
     * 获取所有房屋信息
     */
    @PreAuthorize("@ss.hasPermi('house:house:queryAll')")
    @GetMapping(value = "/all")
    @TenantScope
    public AjaxResult allHouseInfo() {
        return success(houseService.allHouseInfo());
    }
}
